<?php
include './include/common.inc.php';
include '../admin/include/advertis.class.php';
$advertis = new advertis();
$action = isset($action) ? $action : 'manage';
$forward = get_forward('/admin/ads.php');
switch ($action)
{
	case 'add':
		if(isset($_POST['adsname']))
		{
			if(check_perms('ads002')===false)
			{
				$response->alert('您没有此项操作权限！');
				$response->execute();
			}
			if(empty($adsname) || strlen($adsname) > 60)
			{
				$response->text_alert('error','adsname','广告名称必须填写且长度不得大于60个字符长度！');
			}
			$response->real_execute();
			$linkurl = isset($linkurl) ? $linkurl : '';
			$adscontent = isset($adscontent) ? $adscontent : '';
			$file = isset($_FILES['file']) ? $_FILES['file'] : false;
			$res = $advertis->add($adsname,$description,$adstype,$linkurl,$adscontent,'file');
			if($res === false)
			{
				$response->alert('添加失败或上传的文件不符合规定');
			}
			else 
			{
				$response->alert('添加成功');
				$response->redirect($forward);
			}
			$response->execute();
		}
		else 
		{
			if(check_perms('ads002')===false)
			{
				showmessage('您没有此项操作权限！');
			}
			include template('admin', 'ads_add');
		}
		break;
	case 'edit':
		if(isset($_POST['adsname']))
		{
			if(check_perms('ads003')===false)
			{
				$response->alert('您没有此项操作权限！');
				$response->execute();
			}
			if(!isset($aid))
			{
				$response->alert('请选择要修改的广告');
				$response->execute();
			}
			if(empty($adsname) || strlen($adsname) > 60)
			{
				$response->text_alert('error','adsname','广告名称必须填写且长度不得大于60个字符长度！');
			}
			$response->real_execute();
			$linkurl = isset($linkurl) ? $linkurl : '';
			$adscontent = isset($adscontent) ? $adscontent : '';
			$file = isset($_FILES['file']) ? $_FILES['file'] : false;
			$res = $advertis->edit($aid,$adsname,$description,$adstype,$linkurl,$adscontent,'file');
			if($res === false)
			{
				$response->alert('修改失败或上传的文件不符合规定');
			}
			else 
			{
				$response->alert('修改成功');
				$response->redirect($forward);
			}
			$response->execute();
		}
		else 
		{
			if(check_perms('ads003')===false)
			{
				showmessage('您没有此项操作权限！');
			}
			if(!isset($aid))
			{
				showmessage('请选择要修改的广告');
			}
			$info = $advertis->get_advertis_by_id($aid);
			if(!isset($info['adsid']))
			{
				showmessage('对不起，您要修改的广告不存在');
			}
			include template('admin', 'ads_edit');
		}
		break;
	case 'code':
		if(!isset($aid))
		{
			showmessage('请选择要获得代码的广告');
		}
		$info = $advertis->get_advertis_by_id($aid);
		if(!isset($info['adsid']))
		{
			showmessage('对不起，您要获得代码的广告不存在');
		}
		$str = '{ads('.$aid.',\'自定义宽度\',\'自定义长度\')}';
		include template('admin', 'ads_code');
		break;
	case 'delall':
		if(check_perms('ads004')===false)
		{
			$response->alert('您没有此项操作权限！');
			$response->execute();
		}
		if(is_array($ck) && sizeof($ck) > 0)
		{
			foreach ($ck as $v)
			{
				$advertis->edit_advertis($v,array('status'=>STATUS_DEL));
			}
			$response->alert('删除成功');
			$response->script('self.location=self.location');
		}
		else 
		{
			$response->alert('请选择要删除的广告');
		}
		$response->execute();
		break;
	case 'passall':
		if(check_perms('ads005')===false)
		{
			$response->alert('您没有此项操作权限！');
			$response->execute();
		}
		$passed = isset($passed) ? $passed : 0;
		if(is_array($ck) && sizeof($ck) > 0)
		{
			foreach ($ck as $v)
			{
				$advertis->edit_advertis($v,array('passed'=>$passed));
			}
			$response->alert('审核成功');
			$response->script('self.location=self.location');
		}
		else 
		{
			$response->alert('请选择要审核的广告');
		}
		$response->execute();
		break;
	case 'search':
		$url = $self_url;
		$search_params = array();
		if(isset($passed) && intval($passed) >= 0)
		{
			$search_params[] = 'passed='.$passed;
		}
		if(isset($kw) && !empty($kw))
		{
			$search_params[] = 'kw='.urlencode($kw);
		}
		$pos = sizeof($search_params)>0 ? '?' : '';
		$response->redirect($url.$pos.implode('&',$search_params));
		$response->execute();
		break;
	case 'manage':
	default:
		if(check_perms('ads001')===false)
		{
			showmessage('您没有此项操作权限！');
		}
		$page = isset($page) ? $page : 1;
		$page = max(1,intval($page));
		$pagesize = 20;
		$passed = isset($passed) ? $passed : false;
		$kw = isset($kw) ? $kw : false;
		$list = $advertis->get_advertis($page,$pagesize,$passed,$kw,'adsid,adsname,description,inputtime,passed');
		$pages = $advertis->pages;
		$forward = make_forward();
		include template('admin', 'ads_manage');
}

?>